package com.circle.interests.biz.api;

import cn.hutool.json.JSONUtil;
import com.circle.common.service.api.dto.HongLianOrderDto;
import com.circle.common.service.api.service.DubboHongLianCallBackService;
import com.circle.interests.biz.entity.PrizeGameWinRecord;
import com.circle.interests.biz.entity.PrizeHonglianProduct;
import com.circle.interests.biz.enums.PrizeStatusEnum;
import com.circle.interests.biz.service.IPrizeGameWinRecordService;
import com.circle.interests.biz.service.IPrizeHonglianProductService;
import com.circle.messagecenter.api.dto.MessageSendDto;
import com.circle.messagecenter.api.service.DubboMessageCenterService;
import com.circle.prize.library.api.dto.game.PrizeGameWriteOffCallbackDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhou
 * @date 2021/12/22
 */
@Slf4j
@DubboService
public class DubboHongLianCallBackServiceImpl implements DubboHongLianCallBackService {

    @Resource
    private IPrizeGameWinRecordService prizeGameWinRecordService;
    @DubboReference
    private DubboMessageCenterService dubboMessageCenterService;
    @Resource
    private IPrizeHonglianProductService prizeHonglianProductService;

    @Override
    public void phoneFeeCallback(HongLianOrderDto hongLianOrderDto) {
        callback(hongLianOrderDto);
    }

    @Override
    public void virtualPrizeCallback(HongLianOrderDto hongLianOrderDto) {
        callback(hongLianOrderDto);
    }

    private void callback(HongLianOrderDto hongLianOrderDto) {
        log.info("鸿联回调:{}", JSONUtil.toJsonStr(hongLianOrderDto));
        PrizeGameWinRecord record = prizeGameWinRecordService.getByOrderCode(hongLianOrderDto.getOrderCode());
        if (record == null) {
            log.warn("鸿联回调未找到中奖记录:{}", JSONUtil.toJsonStr(hongLianOrderDto));
            return;
        }
        PrizeGameWriteOffCallbackDto dto = new PrizeGameWriteOffCallbackDto();
        dto.setOrderCode(record.getCode());
        dto.setMessage(hongLianOrderDto.getStatusInfo());
        dto.setCardNumber(hongLianOrderDto.getCardNo());
        dto.setCardPassword(hongLianOrderDto.getCardPassword());
        PrizeHonglianProduct product = prizeHonglianProductService.getByProductCode(record.getProductCode());
        if(product != null){
            dto.setUseType(product.getUseType());
        }
        if (Objects.equals(hongLianOrderDto.getStatusCode(), 2)) {
            //充值成功
            record.setWriteOffStatus(PrizeStatusEnum.WRITE_OFF_SUCCESS.getCode());
            record.setCardNumber(hongLianOrderDto.getCardNo());
            record.setCardPassword(hongLianOrderDto.getCardPassword());
            dto.setWriteOffStatus(3);
        } else if (Objects.equals(hongLianOrderDto.getStatusCode(), 2)) {
            // 充值失败
            record.setWriteOffStatus(PrizeStatusEnum.WRITE_OFF_FAIL.getCode());
            dto.setWriteOffStatus(4);
        } else {
            dto.setWriteOffStatus(2);
        }
        prizeGameWinRecordService.updateById(record);
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("json", JSONUtil.toJsonStr(dto));
            MessageSendDto messageSendDto = new MessageSendDto();
            messageSendDto.setMessageCode("prize_library_callback");
            messageSendDto.setSenderId("prizeLibrary");
            messageSendDto.setKeyValue(map);
            dubboMessageCenterService.send(messageSendDto);
        } catch (Exception e) {
            log.info("核销失败");
        }
    }
}
